# -*- coding:utf-8 -*-
# __author__ : "张百科"
# Date: 2021/1/25 14:27
# File: department.py
# IDE: PyCharm
from rest_framework.viewsets import GenericViewSet
from serializers.department import DepartmentSerializer
from models.department import Department
from rest_framework import filters
from django_filters.rest_framework import DjangoFilterBackend
from utils.response.response import BaseResponse
from utils.Decorators.webworkdecorator import WorkDecorator
from utils.response.httpstate import *
from utils.pagination import pagination,pageSerializer
from rest_framework.decorators import action
from rest_framework.schemas import ManualSchema
from coreapi import Field
import coreschema


class DepartmentViewSet(GenericViewSet):

    queryset = Department.objects.filter(is_delete=0)
    serializer_class = DepartmentSerializer
    filter_backends = (DjangoFilterBackend,filters.SearchFilter,filters.OrderingFilter)
    pagination_class =pagination.Pagination
    #filter_class = apifilters.EnterpriseFilter
    search_fields = ('name')

    @WorkDecorator(operate="部门管理", message='部门列表')
    def list(self, request,*args,**kwargs):

        queryset = self.filter_queryset(self.get_queryset())
        page_queryset = self.paginate_queryset(queryset)
        serializer = self.get_serializer(page_queryset, many=True)

        count = self.paginator.page.paginator.count  # 总数
        num = self.paginator.page.paginator.num_pages  # 页数
        page_size = self.paginator.get_page_size(request)  # 每页显示条数
        pageserializer = pageSerializer.Serializer({'count': count, 'page_num': num, 'page_size': page_size})

        return BaseResponse(data=serializer.data,page=pageserializer.data)

    @WorkDecorator(operate="部门管理", message='获取子类列表')
    @action(methods=['get'], detail=False, schema=ManualSchema(
        fields=[
            Field(name='id', location='query', schema=coreschema.String(description='父级ID')),
        ])
            )
    def children(self, request, *args, **kwargs):

        id= request.GET.get('id')

        queryset = self.filter_queryset( Department.objects.filter(is_delete=0,parent_id=id))

        serializer = self.get_serializer(queryset, many=True)



        return BaseResponse(data=serializer.data)

    @WorkDecorator(operate="部门管理", message='新增部门')
    def create(self, request,*args,**kwargs):

        data = request.data
        # 2.创建序列化器
        serializer = self.get_serializer(data=data)
        # 3.校验
        serializer.is_valid(raise_exception=True)
        # 4.保存
        serializer.save()
        # 5.返回响应
        return BaseResponse(data=serializer.data)

    @WorkDecorator(operate="部门管理", message='更新部门')
    def update(self, request,pk,*args,**kwargs):

        object = Department.objects.filter(pk=pk).first()
        object_ser = DepartmentSerializer(object, data=request.data)

        if object_ser.is_valid():
            object_ser.save()
            return BaseResponse()
        else:
            return BaseResponse(message='错误', code=CODE_BUSSINESS_ERROR)
        # 5.返回响应

    @WorkDecorator(operate="部门管理", message='删除部门')
    def destroy(self, request, pk, *args, **kwargs):


        object = Department.objects.filter(pk=pk).first()
        object.is_delete = 1
        object.save()
        return BaseResponse()
